﻿/**
 *    Copyright 2006 - 2011 Opera Software ASA
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 **/


window.cls || ( window.cls = {} );

cls.UserTracker = function(url, storagekey)
{

  this._make_id = function(length)
  {
    length = length || 16;
    var chars = "0123456789abcdefghijklmnopqrstuvwxyz";
    var ret = "";
    while (length--)
    {
        ret += chars.charAt(Math.floor(Math.random() * chars.length))
    }
    return ret;
  }

  this.call_home = function(callback)
  {
    callback = callback || function() {};
    var conn = new XMLHttpRequest();
    var url = this.url + "?user=" + this.id + "&timestamp=" + (new Date().getTime());

    var rshandler = function()
    {
      if (conn.readyState == 4)
      {
        callback(conn.status, url);
      }
    }

    conn.onreadystatechange = rshandler;
    conn.open("GET", url, true);
    conn.send(null);
  }


  this.url = url;
  this.storagekey = "usertracker_id";

  if (window.localStorage)
  {
    this.id = window.localStorage.getItem(this.storagekey);
    if (!this.id)
    {
      this.id = this._make_id(8);
      window.localStorage.setItem(this.storagekey, this.id);
    }
  }
  else
  {
    this.id = "client-without-localstorage";
  }
}
